/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import com.bayu.common.lib.CommonUtil;
import com.bayu.common.lib.RoundNumber;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import com.bayu.controller.MainController;
import com.bayu.services.dao.CompDao;
import com.bayu.services.dao.RejectGalvaImpl;
import com.bayu.services.entity.Comp;
import com.bayu.services.entity.Preinspection;
import com.bayu.services.entity.RejectGalva;
import java.awt.Color;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author Bayu
 */
@Controller
public class CompGraph {

    private String query;
    CompDao compDao;

    @Autowired
    private void setInpectionDao(CompDao compDao){
      this.compDao = compDao;
    }
    @RequestMapping("/comp-graph.png")
    private void compGraph (OutputStream stream) throws Exception{
        /*List inspcumm= compDao.custom("select sum(inspprice) from Comp where date between '"+MainController.startDate+"' and '"+MainController.endDate+"'");
        List rjtcumm= compDao.custom("select sum(rjtprice) from Comp where date between '"+MainController.startDate+"' and '"+MainController.endDate+"'");

        List insp= compDao.custom("select inspprice from Comp where date ='"+MainController.endDate+"'");
        List rjt= compDao.custom("select rjtprice from Comp where date='"+MainController.endDate+"'");
        //Cumm
        String strInspCumm = inspcumm.get(0).toString();
        String strRjtCumm = rjtcumm.get(0).toString();
        double dInspCumm = Double.valueOf(strInspCumm).doubleValue();
        double dRjtCumm = Double.valueOf(strRjtCumm).doubleValue();
        double persenRjtCum =(dRjtCumm / dInspCumm )*100;

        //Not
        String strInsp = insp.get(0).toString();
        String strRjt = rjt.get(0).toString();
        double dInsp = Double.valueOf(strInsp).doubleValue();
        double dRjt = Double.valueOf(strRjt).doubleValue();
        double persenRjt = (dRjt/dInsp)*100;

        double max;
        if(persenRjt > persenRjtCum){
            max = persenRjt + 10 ;
        }else{
            max = persenRjtCum + 10;
        }

        System.out.println(max+"-"+persenRjt+"-"+persenRjtCum);
*/
        
    
        
        
        Double min = Double.parseDouble("0");
        Double max = Double.parseDouble("20");
        
        JFreeChart chart = getCompGraph(min, max);
        ChartUtilities.writeChartAsPNG(stream, chart, 1480,800);
    }
    private JFreeChart getCompGraph(Double min,Double max){
       DefaultCategoryDataset dataset = dataset();
       String title="COMPONEN REJECTION TOTAL";
        String tgl1="";
      String tgl2="";


      if(!MainController.startDate.equals("")){
          tgl1 = CommonUtil.formatStringDate(MainController.startDate);
          tgl2 = CommonUtil.formatStringDate(MainController.endDate);
          title = title+"\r\n"+ tgl1+"-"+tgl2;
      }
       final JFreeChart chart = ChartFactory.createLineChart(
            title,        // chart title
           "Date",               // domain axis label
           "(% Daily)",                  // range axis label
            dataset,                 // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips?
            false                     // URL generator?  Not required...
        );
        chart.setBackgroundPaint(new Color(255, 255, 255));
        CategoryPlot plot = chart.getCategoryPlot();

        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

        CategoryItemRenderer renderer1 = plot.getRenderer();
        renderer1.setSeriesPaint(0, Color.BLUE);
        renderer1.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER));
        renderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer1.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        renderer1.setBaseItemLabelsVisible(true);
/*
        DefaultCategoryDataset dataset2 = inspDataset2();
        ValueAxis axis2 = new NumberAxis("% Accum");
        axis2.setAutoRange(false);
        axis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        axis2.setRange(min,max);

       plot.setRangeAxis(1, axis2);

    plot.setDataset(1, dataset2);
    plot.mapDatasetToRangeAxis(1, 1);
    CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
    renderer2.setSeriesPaint(0, Color.blue);
    renderer2.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer2.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
    renderer2.setBaseItemLabelsVisible(true);
    plot.setRenderer(1, renderer2);

    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
*/
        NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
        rangeAxis.setAutoRange(false);
        rangeAxis.setRange(min,max);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

       return chart;
    }


    public DefaultCategoryDataset dataset(){
        String name1="% Rjt";
        String name2 = "% Rjt (Cumm)";
        DefaultCategoryDataset data1 = new DefaultCategoryDataset();
        List data = this.compDao.getAllByDate(MainController.startDate, MainController.endDate);
        //List data = this.rejectGalvaDao.getAllByDate("2010-12-01", "2010-12-31");
        Double rjtcum = 0.0;
        Double inspcum = 0.0;
        NumberFormat formatter = new DecimalFormat("0.0");
        for(int i=0;i<data.size();i++){
          Comp c = (Comp)data.get(i);
          String cat[] = c.getDate().toString().split("-");
          rjtcum = rjtcum + c.getRjtprice();
          inspcum = inspcum + c.getInspprice();
          Double percentRjt = (c.getRjtprice() / c.getInspprice()) * 100 ;
          Double percentRjtCum = (rjtcum / inspcum) * 100;
          //String sPercentRjt = formatter.format(percentRjt);
          //String sPercentRjtCum = formatter.format(percentRjtCum);


          data1.addValue(RoundNumber.getRound(percentRjt, 1), name1, cat[2]);
          data1.addValue(RoundNumber.getRound(percentRjtCum, 1), name2, cat[2]);
          
        }

        return data1;
    }




}
